from window import Window, Event
from enum import Enum

################################################################################
# Graph2D
# The basic class of 2D graph
################################################################################


class EventGraphStatus(Enum):
    NORMAL = 0
    PRESSED = 1
    RELEASED = 2
    OVER = 3


class ButtonStatus(Enum):
    NORMAL = 0
    SELECTED = 1
    DISABLED = 2


class Graph2D(Event):
    def __init__(self, x: int, y: int, w: int, h: int):
        self.loc_x = x
        self.loc_y = y
        self.__width = w
        self.height = h
        self.__visible = True
        self.__dirty = True  # default is dirty
        self.__name = ''
        self.__status = EventGraphStatus.NORMAL  # 0: normal, 2: pressed
        pass

    def draw(self):
        print('Graph2D draw')

    def loction(self):
        return (self.loc_x, self.loc_y)

    def setBgColor(self):
        self.dirty = True
        pass

    @property
    def width(self) -> int:
        return self.__width

    @width.setter
    def width(self, width: int):
        self.__width = width

    @property
    def visible(self) -> bool:
        return self.__visible

    @visible.setter
    def visible(self, visible: bool):
        self.dirty = True
        self.__visible = visible

    @property
    def dirty(self) -> bool:
        return self.__dirty

    @dirty.setter
    def dirty(self, dirty: bool):
        self.__dirty = dirty

    @property
    def name(self) -> str:
        return self.__name

    @name.setter
    def name(self, name):
        self.__name = name

    @property
    def status(self) -> int:
        return self.__status

    @status.setter
    def status(self, status):
        if self.__status == status:
            return
        # status changed set dirty as True
        self.dirty = True
        self.__status = status

    def onMouseClick(self, x: int, y: int):
        if self.inRange(x, y) and self.visible:
            print(self.name, ' Graph2D mouse event')

    def inRange(self, x: int, y: int):
        if((x > self.loc_x) and x < (self.loc_x + self.width)):
            if ((y > self.loc_y) and y < (self.loc_y + self.height)):
                return True
        return False

# Pixel


class Pixel(Graph2D):
    def __init__(self, x: int, y: int):
        super().__init__(x, y, 1, 1)

    def draw(self, win: Window):
        win.game.draw.rect(win, win.game.Color(
            'red'), (self.loc_x, self.loc_y, self.width, self.height), width=0)

# Button


class Button(Graph2D):

    button_id = int(0)

    def __init__(self, text: str, x: int, y: int, w: int, h: int):
        super().__init__(x, y, w, h)
        Button.button_id += 1
        self.__click = None
        self.__press_bg_color = (100, 100, 100, 255)
        self.__normal_bg_color = (255, 0, 0, 255)
        self.__text = text
        self.__id = (self.button_id)
        self.name = str('Button_'+str(self.__id))
        self.label = Label(text, self.loc_x+5, self.loc_y+5, 1)

    def draw(self, win: Window):
        if self.visible:
            print('Button draw', self.name)
            face = win.game.Surface((self.width, self.height)).convert_alpha()

            if self.status == EventGraphStatus.PRESSED:
                bg = self.__press_bg_color
            else:
                bg = self.__normal_bg_color

            win.game.draw.rect(face, bg, (0, 0,
                               self.width, self.height), width=0, border_radius=5)
            win.screen.blit(face, (self.loc_x, self.loc_y))

            # draw label
            self.label.draw(win)

        self.dirty = False

    @property
    def text(self):
        return self.__text

    @text.setter
    def text(self, text: str):
        self.__text = text

    def onClick(self, click):
        self.__click = click

    def onMousePress(self, x: int, y: int):
        if self.inRange(x, y) and self.visible:
            self.status = EventGraphStatus.PRESSED
        return super().onMousePress(x, y)

    def onMouseRelease(self):
        self.status = EventGraphStatus.RELEASED
        return super().onKeyRelease()

    def onMouseClick(self, x: int, y: int):
        if self.inRange(x, y) and self.visible:
            if self.__click:
                self.__click()
        return super().onMouseClick(x, y)

# Image


class Image(Graph2D):

    def __init__(self, x: int, y: int, w: int, h: int):
        super().__init__(x, y, w, h)
        pass

# BitMapImage


class BitMapImage(Image):
    def __init__(self, x: int, y: int, path: str):
        super().__init__(x, y, 50, 50)
        self.__path = path
        pass

    @property
    def path(self) -> str:
        return self.__path

    @path.setter
    def path(self, path: str):
        self.__path = path

    def draw(self, win: Window):
        if not self.visible:
            return
        face = win.game.Surface((self.width, self.height)).convert_alpha()
        for h in range(0, self.height):
            for w in range(0, self.width):
                bg = ((w*h) % 255, (w*h) % 255, 0, (w*h) % 255)
                win.game.draw.rect(face, bg, (w, h, 1, 1), width=0)
        win.screen.blit(face, (self.loc_x, self.loc_y))
        self.dirty = False

# Line


class Line(Graph2D):
    pass


# Label
class Label(Graph2D):
    def __init__(self, text: str, x: int, y: int, size: int):
        super().__init__(x, y, size, size)
        self.__text = text

    @property
    def text(self):
        return self.__text

    @text.setter
    def text(self, text: str):
        self.__text = text

    def draw(self, win: Window):
        win.game.font.init()
        myfont = win.game.font.SysFont('Comic Sans MS', 20)
        textsurface = myfont.render(self.text, True, (0, 0, 0))
        win.screen.blit(textsurface, (self.loc_x, self.loc_y))


class TextBox(Graph2D):
    def __init__(self, x: int, y: int, w: int, h: int):
        super().__init__(x, y, w, h)

    def draw(self, win: Window):
        # draw outline
        pass

# Box


class Box():
    pass

# Dialog


class Dialog():
    pass
